package com.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.store.domain.R;
import com.store.domain.dto.BookDto;
import com.store.domain.entity.Book;
import com.store.mapper.BookMapper;
import com.store.service.BookService;
import com.store.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 图书表(Book)表服务实现类
 *
 * @author yu
 * @since 2024-06-26 01:12:42
 */
@Service("bookService")
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Override
    public R getPageBook(Integer pageNum, Integer pageSize, BookDto bookDto) {
        LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.or().like(bookDto.getQuery() != null, Book::getName, bookDto.getQuery());
        queryWrapper.or().like(bookDto.getQuery() != null, Book::getAuthor, bookDto.getQuery());
        queryWrapper.or().like(bookDto.getQuery() != null, Book::getPublisher, bookDto.getQuery());
        queryWrapper.eq(bookDto.getCategoryId() != null, Book::getCategoryId, bookDto.getCategoryId());
        Page<Book> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);
        return R.success(page.getTotal(), page.getRecords());
    }

    @Override
    public R addBook(BookDto bookDto) {
        Book book = BeanCopyUtils.copyBean(bookDto, Book.class);
        if (save(book)) {
            return R.success("添加成功");
        }
        return R.error();
    }

    @Override
    public R deleteBookById(Integer id) {
        if (removeById(id)) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @Override
    public R getBookById(Integer id) {
        Book book = getById(id);
        return R.success(book);
    }

    @Override
    public R updateBook(BookDto bookDto) {
        Book book = BeanCopyUtils.copyBean(bookDto, Book.class);
        if (updateById(book)) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    @Override
    public R updateBookStatus(Integer id, Integer status) {
        Book book = new Book();
        book.setId(id);
        book.setStatus(status);
        if (updateById(book)) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    @Override
    public R deleteBatchByIds(Integer[] ids) {
        if (removeByIds(Arrays.asList(ids))) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @Override
    public R showBooks() {
        //热门推荐逻辑
        List<Book> list = list();
        return R.success(list);
    }

    @Override
    public R searchBook(String query) {
        if (query == null || query.equals("")){
            return R.error("请输入搜索内容");
        }
        LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.or().like(Book::getName, query);
        queryWrapper.or().like(Book::getAuthor, query);
        queryWrapper.or().like(Book::getPublisher, query);
        List<Book> list = list(queryWrapper);
        return R.success(list);
    }
}


